Hi!
You can try to use a Script Unit in order to accomplish your task.
There are some useful articles in our WebRatio Wiki:
- Getting started with the Script unit
- Getting started with Groovy
- Script units accessing database
- Querying with Hibernate
Let's make a simple example on how you can write the Groovy script to perform your task.
Suppose you have an uploaded file and you want to save the content of this file inside one Entity of your Data Model.
In particular consider that each row of the uploaded file has a value and you want to save this value in the attribute ""Value"" of your Entity (i.e. ""LogRecord"").
So your script should perform these tasks:
1) Take as input the uploaded file
2) Read the uploaded file
3) Parse each row of the file
4) Save the content of each row in the entity LogRecord
First of all you have to define the input of the script (i.e. the uploaded file):
#input RTXBLOBData file
Then let's import some packages that you will use
import java.io.*
import com.webratio.webapp.LogRecord
The last import is the class which represents the JavaBean of the entity ""LogRecord""
Now you have to recover the database session
/*Recover db session
Suppose that our database id is ""db1""
*/
def dbId = ""db1""
def session = getDBSession(dbId)
At this point you have to create a File object from the input file, read it and save its content to the entity ""LogRecord""
try
{
/*Create temporary file to parse from RTXBLOBData*/
def tempFile=new File(file.getName());
InputStream inputStream= file.openFileInputStream()
OutputStream outStream=new FileOutputStream(tempFile);
def buf=new byte[1024]
def len
while((len=inputStream.read(buf))>0)
{
outStream.write(buf,0,len)
}
Now you have the temporary File object that can be read row by row
/*Read temporary file row by row until !EoF*/
tempFile.eachLine{i->
/*Parse your file row*/
In our case each row has only one value
/*Save data*/
//Create an instance of our entity
LogRecord log = new LogRecord()
//Use the setter method exposed by the JavaBean to set the value
log.setValue(i)
//Save the data in the entity
session.save(log)
}
//Close the InputStream and OutputStream instances in order to release any system resources associated with the streams
outStream.close()
inputStream.close()
//Perform the commit of the database session in order to physically write the changes on the database
commit(session)
}catch(Exception e){
println(e.getMessage())}